//加载from模块,如果不加载，select、checkbox、radio等将无法显示，并且无法使用form相关功能  
var from;
var laydate;
layui.use(['form','laydate'], function(){
    var form = layui.form;
    var laydate = layui.laydate;

    laydate.render({
      elem: '#birthday'
      ,format: 'yyyy/MM/dd'
    });
  

  $(function(){
    //呈现数据
    init();
  });

  //数据呈现
  function init(){
    //获取本地存储的数据
    let tuser=JSON.parse(localStorage.getItem("key"));
    //循环数据
    one:
    for(let i=0;i<tuser.length;i++){
      //拿登录员工的id
      let tId=JSON.parse(localStorage.getItem("tId"));
      two:
      for(let j=0;j<tId.length;j++){
          let loginId=tId[0].id;
          // console.log(loginId);  //拿到当前员工的id
          if(loginId==tuser[i].id){
              //文本框显示的内容
              form.val("change",{
                "id":tuser[i].id,
                "name":tuser[i].userName,
                "qq":tuser[i].QQ,
                "mail":tuser[i].Email,
                "tel":tuser[i].tel,
                "birthday":tuser[i].birthday,
                "age":tuser[i].age,
                "emergency":tuser[i].emergency,
                "number":tuser[i].number,
                "address":tuser[i].address,
                "sex":tuser[i].sex,
                "marriage":tuser[i].marriage,
                "department":tuser[i].department,
                "position":tuser[i].position,
                "educational":tuser[i].educational
              });
              break one;
          }
      }
    }
  }

  //修改数据
  $("#change").on("click",function(){
    //获取本地存储的数据
    let tuser=JSON.parse(localStorage.getItem("key"));
    let Leave=JSON.parse(localStorage.getItem("myleave"));
    //拿到表单元素的值
    let data = form.val('change');
    //正则规则
    let regQQ=/^(\d){6,10}$/
    let regMail=/(\w){1,11}@(\w)+.(\w){2,}$/
    let regPhone=/^(\d){6,11}$/
    let regEmer=/^(\d){6,11}$/
    let regNumber=/^(\d){18}$/
    // console.log(data.id);
    //修改职工数据
    for(let i=0;i<tuser.length;i++){
      if(regQQ.test(data.qq)==true && regMail.test(data.mail)==true && regPhone.test(data.tel)==true && regEmer.test(data.emergency)==true && regNumber.test(data.number)==true){
        if(tuser[i].id==data.id){
          tuser[i].tel=data.tel;
          tuser[i].mail=data.mail;
          tuser[i].userName=data.name;
          tuser[i].birthday=data.birthday;
          tuser[i].sex=data.sex;
          tuser[i].QQ=data.qq;
          tuser[i].emergency=data.emergency;
          tuser[i].department=data.department;
          tuser[i].position=data.position;
          tuser[i].number=data.number;
          tuser[i].address=data.address;
          tuser[i].marriage=data.marriage;
          tuser[i].educational=data.educational;
          tuser[i].age=data.age;
        }
      }else if(regQQ.test(data.qq)==false){
        alert("QQ格式不正确，请重新输入！");
        break;
      }else if(regMail.test(data.mail)==false){
        alert("邮箱输入格式不正确，请重新输入！");
        break;
      }else if (regPhone.test(data.tel)==false) {
        alert("联系方式输入格式不正确，请重新输入！");
        break;
      }else if(regEmer.test(data.emergency)==false){
        alert("紧急联系方式输入格式不正确，请重新输入！");
        break;
      }else if(regNumber.test(data.number)==false){
        alert("身份证号码输入格式不正确，请重新输入！");
        break;
      }
    }
    //修改请假数据
    for(let j=0;j<Leave.length;j++){
      if(Leave[j].id==data.id){
        Leave[j].userName=data.name;
        Leave[j].department=data.department;
      }
    }
    //将最新的数据保存在本地
    localStorage.setItem("key",JSON.stringify(tuser));
    localStorage.setItem("myleave",JSON.stringify(Leave));
    //刷新
    init();
  });
});